package com.github.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/21 上午9:43
 */
public class BoundBuffer2 {

    public static void main(String[] args) throws InterruptedException {

        Data data = new Data();

        for(int i = 0;i < 2;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        data.put(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        TimeUnit.SECONDS.sleep(3);

        for (int i = 0;i < 1;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        data.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    static class Data{
        final Lock lock = new ReentrantLock();
        final Condition notFull = lock.newCondition();
        final Condition notEmpty = lock.newCondition();
        final Object[] items = new Object[1];
        int putptr, takeptr, count;

        // 生产者方法，往数组里面写数据
        public void put(Object x) throws InterruptedException {
            lock.lock();
            try {
                while (count == items.length) {
                    notFull.await(); //数组已满，没有空间时，挂起等待，直到数组“非满”（notFull）
                }
                items[putptr] = x;
                if (++putptr == items.length) {
                    putptr = 0;
                }
                ++count;
                // 因为放入了一个数据，数组肯定不是空的了
                // 此时唤醒等待这notEmpty条件上的线程
                notEmpty.signal();
            } finally {
                lock.unlock();
            }
        }

        // 消费者方法，从数组里面拿数据
        public Object take() throws InterruptedException {
            lock.lock();
            try {
                while (count == 0) {
                    notEmpty.await(); // 数组是空的，没有数据可拿时，挂起等待，直到数组非空（notEmpty）
                }
                Object x = items[takeptr];
                if (++takeptr == items.length) {
                    takeptr = 0;
                }
                --count;
                // 因为拿出了一个数据，数组肯定不是满的了
                // 此时唤醒等待这notFull条件上的线程
                notFull.signal();
                return x;
            } finally {
                lock.unlock();
            }
        }
    }
}